package mygrid;

import java.util.regex.Pattern;
import java.util.concurrent.TimeUnit;
import org.testng.*;
import org.testng.annotations.*;
import static org.junit.Assert.*;
import static org.hamcrest.CoreMatchers.*;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;

public class AnPozit extends mygrid.pages.TestBase {
  private boolean acceptNextAlert = true;
  private StringBuffer verificationErrors = new StringBuffer();

  @Test
  public void testAnpozit() throws Exception {    driver.get("https://seolib.ru/tools/positions/analysis/");
    for (int second = 0;; second++) {
    	if (second >= 60) fail("timeout");
    	try { if (isElementPresent(By.xpath("//*[@id='lblurlsite']"))) break; } catch (Exception e) {}
    	Thread.sleep(1000);
    }

    driver.findElement(By.id("urlsite")).clear();
    driver.findElement(By.id("urlsite")).sendKeys("http://ukr.net");
    driver.findElement(By.id("keywords")).clear();
    driver.findElement(By.id("keywords")).sendKeys("UKR.NET: Всі новини України, останні новини дня в Україні та Світі");
    driver.findElement(By.xpath("//button[@value='500']")).click();
    driver.findElement(By.name("add_region")).click();
    for (int second = 0;; second++) {
    	if (second >= 60) fail("timeout");
    	try { if (isElementPresent(By.cssSelector("div.row > div.col-sm-8"))) break; } catch (Exception e) {}
    	Thread.sleep(1000);
    }

    driver.findElement(By.cssSelector("div.col-sm-8 > input.form-control")).clear();
    driver.findElement(By.cssSelector("div.col-sm-8 > input.form-control")).sendKeys("Киев");
    driver.findElement(By.xpath("//*[@id='regions_modal_yandex']//button[contains(@class,'regions-confirm')]")).click();
    driver.findElement(By.id("google")).click();
    driver.findElement(By.cssSelector("#selected_google_region > span.space50 > a[name=\"add_region\"]")).click();
    for (int second = 0;; second++) {
    	if (second >= 60) fail("timeout");
    	try { if (isElementPresent(By.xpath("//div[@id='regions_modal_google']"))) break; } catch (Exception e) {}
    	Thread.sleep(1000);
    }

    driver.findElement(By.xpath("//div[@id='regions_modal_google']//div[@class='col1']/div[5]")).click();
    driver.findElement(By.xpath("//*[@id='regions_modal_google']//button[contains(@class,'regions-confirm')]")).click();
    driver.findElement(By.id("mail")).click();
    driver.findElement(By.id("yahoo")).click();
    driver.findElement(By.id("bing")).click();
    driver.findElement(By.id("submit_btn")).click();
    for (int second = 0;; second++) {
    	if (second >= 60) fail("timeout");
    	try { if ("Сниппет".equals(driver.findElement(By.xpath("//div[@id='tab_0']/div/table/thead/tr/th[4]")).getText())) break; } catch (Exception e) {}
    	Thread.sleep(1000);
    }

  }

  private boolean isElementPresent(By by) {
    try {
      driver.findElement(by);
      return true;
    } catch (NoSuchElementException e) {
      return false;
    }
  }

  private String closeAlertAndGetItsText() {
    try {
      Alert alert = driver.switchTo().alert();
      String alertText = alert.getText();
      if (acceptNextAlert) {
        alert.accept();
      } else {
        alert.dismiss();
      }
      return alertText;
    } finally {
      acceptNextAlert = true;
    }
  }
}
